package spider.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import spider.dao.TbConfigDao;
import spider.entity.TbConfig;
import spider.service.SpiderService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author Administrator
 */
@Service
public class SpiderServiceImpl implements SpiderService {
    @Autowired
    TbConfigDao tbConfigDao;
    @Autowired
    SpiderData spiderData;

    @Override
    public Map<String, Object> start() {
        spiderData.start();
        return null;
    }

    @Override
    public Map<String, Object> addConfig(TbConfig tbConfig) {
        String uuid = UUID.randomUUID().toString();
        tbConfig.setId(uuid);
        tbConfigDao.insertSelective(tbConfig);
        Map<String, Object> map = new HashMap<>(16);
        map.put("code", "200");
        map.put("date", uuid);
        return map;
    }

    @Override
    public Map<String, Object> updateConfig(TbConfig tbConfig) {
        Map<String, Object> map = new HashMap<>(16);
        map.put("code", "100");
        TbConfig tbConfig1 = tbConfigDao.selectByPrimaryKey(tbConfig.getId());
        if (tbConfig1 == null) {
            map.put("message", "此配置不存在");
            return map;
        }
        tbConfigDao.updateByPrimaryKeySelective(tbConfig);
        map.put("code", "200");
        map.put("message", "修改成功");
        return map;
    }

    @Override
    public Map<String, Object> deleteConfig(String id) {
        Map<String, Object> map = new HashMap<>(16);
        Integer count = tbConfigDao.deleteByPrimaryKey(id);
        map.put("code", "200");
        map.put("message", "删除成功");
        return map;
    }

    @Override
    public Map<String, Object> selectConfigById(String id) {
        Map<String, Object> map = new HashMap<>(16);
        TbConfig tbConfig = tbConfigDao.selectByPrimaryKey(id);
        if (tbConfig == null) {
            map.put("code", "100");
            map.put("message", "此配置不存在");
            return map;
        }
        map.put("code", "200");
        map.put("data", tbConfig);
        return map;
    }

    @Override
    public Map<String, Object> showConfig() {
        List<TbConfig> list = tbConfigDao.listConfig();
        Map<String, Object> map = new HashMap<>(16);
        map.put("code", "200");
        map.put("date", list);
        return map;
    }
}
